//
//  SXYSNewsMainViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXYSNewsMainViewController.h"
#import "SXRequestManager.h"
#import "SXDataBaseManager.h"
#import "SXYSNewsMainCell.h"
#import "SXYSDetailViewController.h"
@interface SXYSNewsMainViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SXYSNewsMainViewController
- (UITableView *)mainTableView
{
    if (_mainTableView == nil) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 143)];
        _mainTableView.dataSource = self;
        _mainTableView.delegate = self;
        [_mainTableView registerClass:[SXYSNewsMainCell class] forCellReuseIdentifier:@"SXYSNewsMainCell"];
    }
    return _mainTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _start = 0;
    _limit = 1;
    [SXDataBaseManager DBManage:nil dataName:_URLString manageType:dataBaseManageTypeQueryData getDataFromDict:^(NSData *data) {
        if (data) {
            [self analyzeData:data];
        }
    }];
    [self requestData];
    // Do any additional setup after loading the view.
}
// 网络请求
- (void)requestData
{
    NSString *URLStr = [kYSNewsMainListURLString stringByAppendingString:_URLString];
    URLStr = [URLStr stringByAppendingString:[NSString stringWithFormat:@"%ld", _start + 1]];
    [SXRequestManager requestWithType:GET urlString:URLStr parDic:nil finish:^(NSData *data) {
        if (_start == 0) {
            [SXDataBaseManager DBManage:data dataName:_URLString manageType:dataBaseManageTypeStoreData getDataFromDict:nil];
        }
        [self analyzeData:data];
        [self removeLoadingView];
    } error:^(NSError *error) {
        NSLog(@"%@", error);
        [self removeLoadingView];
    }];
}
- (void)analyzeData:(NSData *)data
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if (_start == 0) {
        [self.dataSourceArray removeAllObjects];
    }
    for (NSDictionary *YSNewsDic in dic[@"result"][@"rst"]) {
        SXYSNewsMainModel *model = [[SXYSNewsMainModel alloc] init];
        [model setValuesForKeysWithDictionary:YSNewsDic];
        [self.dataSourceArray addObject:model];
    }
    [self.mainTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXYSNewsMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXYSNewsMainCell" forIndexPath:indexPath];
    cell.configureModel = self.dataSourceArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kScreenWidth - 10) * 183 / 320 + 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXYSNewsMainModel *model = self.dataSourceArray[indexPath.row];
    SXYSDetailViewController *detailVC = [[SXYSDetailViewController alloc] init];
    detailVC.idStr = model.Id;
    detailVC.titleStr = model.title;
    [self.rootRightVC.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
